package net.beaconcontroller.comp416project.view;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

/**
 * 
 * @author SHI ZHONGQI 10836265D from HK PolyU
 * @author JI ZHIHAO 13022865X from HK PolyU
 *
 */
public class MutipleDiagram extends JFrame implements Runnable {   
	/**
	 * 
	 */
	JPanel container;
	ChartPanel chartpanel;
	int length;
	int maxmiumDataLength=0;
	private TimeSeries[] timeseries;
	private String[] JTA_Content;
	private boolean[] getData;
	double[] lastValue;
	JFreeChart jfreechart;
	TimeSeriesCollection timeseriescollection;
	Thread thread1;
	static Class<?> class$org$jfree$data$time$Millisecond;
	static int times=0;
	public  TimeSeriesCollection getTimeSeriesCollection(){
		return timeseriescollection;
	}
	@SuppressWarnings("deprecation")
	public MutipleDiagram(int length) {   
		thread1 = new Thread(this);
		JTA_Content = new String[length];
		timeseries = new TimeSeries[length];
		getData = new boolean[length];
		lastValue= new double[length];
		this.length=length;
		timeseriescollection = new TimeSeriesCollection();
		
		for(int i=0;i<length;i++)
		{
			timeseries[i] = new TimeSeries("Port"+(i+1),MutipleDiagram.class$org$jfree$data$time$Millisecond != null ? MutipleDiagram.class$org$jfree$data$time$Millisecond : (MutipleDiagram.class$org$jfree$data$time$Millisecond = MutipleDiagram.getClass("org.jfree.data.time.Millisecond")));
			JTA_Content[i] = "";
			getData[i] = false;
			timeseriescollection.addSeries(timeseries[i]);
			lastValue[i]=0;
		}
		buildUI();
	} 
	

	public void buildUI(){
		this.setSize(1080, 700);
		jfreechart = createChart(timeseriescollection);
		chartpanel = new ChartPanel(jfreechart);
	    //chartpanel.setPreferredSize(new Dimension(500,270));
	    container = new JPanel(new BorderLayout());
	    this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
	    //jpanel.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));//�߾�Ϊ4
	   // jbutton.addActionListener(this);
	    container.add(chartpanel,BorderLayout.CENTER);
	    add(container);
		
	}
	
	  public void update(String s, int id){
			s=s.toUpperCase();
			s= System.currentTimeMillis()+" "+s+"\r\n";
			JTA_Content[id]+=s;
			//System.out.println(s);
		}
	  
	  public void setData(double a,int id){
		  lastValue[id]=a;
		  //System.out.println(a);
		  
		  Millisecond millisecond = new Millisecond();
		  timeseries[id].addOrUpdate(millisecond, lastValue[id]);
		  if(timeseries[id].getItemCount()>this.maxmiumDataLength)
			  this.maxmiumDataLength=timeseries[id].getItemCount();
		  getData[id]=true;
	  }
	  
	  public void run(){
	    while(true){
	    //System.out.println("asdf");
	    try{

	    	for(int i=0;i<length;i++){
	    		while(timeseries[i].getItemCount()<maxmiumDataLength)
	    		{
	    			timeseries[i].addOrUpdate(new Millisecond(), lastValue[i]);
	    		}
	    	}
	    	chartpanel.repaint();
	    	Thread.sleep(250);
	    
	      }catch(Exception e){}
	    }
	  }

	  public void startThread(){
	    thread1.start();
	  }

	  public void actionPerformed(ActionEvent e){
	    if(e.getActionCommand().equals("EXIT")){
	      thread1.interrupt();
	      System.exit(0);
	    }
	  }
	
	  
	  public String getContent(int id){
		  if(id>=length)
			  return null;
		  return JTA_Content[id];
	  }
	  
	  public boolean getData(int id){
		  if(id>=length)
			  return false;
		  return getData[id];
	  }
	  
	  
	 private JFreeChart createChart(XYDataset xydataset){
		    JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Data","Time","Value",xydataset,true,true,true);
		    XYPlot xyplot = jfreechart.getXYPlot();
		    ValueAxis valueaxis = xyplot.getDomainAxis();
		    valueaxis.setAutoRange(true);
		    valueaxis.setFixedAutoRange(60000D);	    
		    valueaxis = xyplot.getRangeAxis();
		    return jfreechart;
		  }
	
	
	 static Class<?> getClass(String s){
		    Class<?> cls = null;
		    try{
		      cls = Class.forName(s);
		    }catch(ClassNotFoundException cnfe){
		      throw new NoClassDefFoundError(cnfe.getMessage());
		    }
		    return cls;
		  }
	
  
}  